package com.eso.frame.utils;

import java.security.MessageDigest;

/**
 * 字符串加密工具类
 * User: Noah.Rong
 * Version: 0.1,  2012-05-21 19:48
 */
public class SecurityUtils {

    /**
     * @param str:summ 加密算法 mdt/sha-1 str待加密字符串
     * @return :加密后字符串
     */
    public static String getMD5String(String summ, String str) {
        try {
            byte[] res = str.getBytes();
            MessageDigest md = MessageDigest.getInstance(summ.toUpperCase());
            for (int i = 0; i < res.length; i++) {
                md.update(res[i]);
            }
            byte[] hash = md.digest();
            StringBuffer d = new StringBuffer("");
            for (int i = 0; i < hash.length; i++) {
                int v = hash[i] & 0xFF;
                if (v < 16)
                    d.append("0");
                d.append(Integer.toString(v, 16).toUpperCase() + "");
            }
            return d.toString();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取字符串的MD5加密串
     * @param str 要加密的字符串
     * @return 加密后的字符串
     */
    public static String getMD5String(String str) {
        return getMD5String("md5", str);
    }

    /**
     * 获取SHA加密字符串
     * @param str 要加密的字符串
     * @return 加密后的字符串
     */
    public static String getSHAString(String str) {
        return getMD5String("SHA-1", str);
    }
}
